/*
 * Copyright 2014 http://Bither.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.photoeditor.demo.util;

import android.content.Context;
import android.graphics.Bitmap;

import com.photoeditor.demo.util.gallery.util.ImageWorker;

import java.io.File;

/**
 * fetched from a URL.
 */
public class ImageFetcher extends ImageWorker {
	private static final String TAG = "ImageFetcher";
	public static final String THUMB_CACHE_DIR = "thumbnail";
	private File mThumbCacheDir;
	protected int mImageWidth;
	protected int mImageHeight;
	
	private void init(Context context) {
		mThumbCacheDir = ImageCache.getDiskCacheDir(context, THUMB_CACHE_DIR);
		if (!mThumbCacheDir.exists()) {
			mThumbCacheDir.mkdirs();
		}
	}
	/**
	 * Initialize providing a target image width and height for the processing
	 * images.
	 * 
	 * @param context
	 * @param imageWidth
	 * @param imageHeight
	 */
	public ImageFetcher(Context context, int imageWidth, int imageHeight) {
		super(context);
		setImageSize(imageWidth, imageHeight);
		init(context);
	}

	/**
	 * Initialize providing a single target image size (used for both width and
	 * height);
	 * 
	 * @param context
	 * @param imageSize
	 */
	public ImageFetcher(Context context, int imageSize) {
		super(context);
		setImageSize(imageSize);
		init(context);
	}
	
	/**
	 * Set the target image width and height.
	 * 
	 * @param width
	 * @param height
	 */
	public void setImageSize(int width, int height) {
		mImageWidth = width;
		mImageHeight = height;
	}

	/**
	 * Set the target image size (width and height will be the same).
	 * 
	 * @param size
	 */
	public void setImageSize(int size) {
		setImageSize(size, size);
	}

	/**
	 * The main process method, which will be called by the ImageWorker in the
	 * AsyncTask background thread.
	 * 
	 * @param data
	 *            The data to load the bitmap, in this case, a regular http URL
	 * @return The downloaded and resized bitmap
	 */
	private Bitmap processBitmap(String data, int degree, 
			AsyncTask<Object, Object, Bitmap> bitmapWorkerTask)
			{

//		if (Loger.isD()) {
//			Loger.d(TAG, "processBitmap - " + data);
//		}
		
		Bitmap bitmap = null;
		
		MediaFile.MediaFileType fileType = MediaFile.getFileType(data);
		if ( fileType == null ) {
			return null;
		}
		if ( MediaFile.isVideoFileType(fileType.fileType) ) {
			bitmap = MediaThumbnailUtil.getInstance().getVideoThumbnail(data, mImageWidth <= ImageManageUtil.SMALL_IMAGE_WIDTH ? ImageManageUtil.MIDDLE_IMAGE_WIDTH : mImageWidth);
		} else if (MediaFile.isImageFileType(fileType.fileType)) {
			bitmap = MediaThumbnailUtil.getInstance().getImageThumbnail(data, mImageWidth, degree);
		} else if(MediaFile.isEncryptImageType(fileType.fileType)){
			bitmap = MediaThumbnailUtil.getInstance().getEncryptImageThumbnail(data, mImageWidth, degree);
		} else if(MediaFile.isEncryptVideoType(fileType.fileType)){
			bitmap = MediaThumbnailUtil.getInstance().getVideoThumbnail(data, mImageWidth <= ImageManageUtil.SMALL_IMAGE_WIDTH ? ImageManageUtil.MIDDLE_IMAGE_WIDTH : mImageWidth);
		}
		return bitmap;
	}
	
	@Override
	protected Bitmap processBitmap(Object data, int degree, 
			AsyncTask<Object, Object, Bitmap> bitmapWorkerTask)
			{
		return processBitmap(String.valueOf(data), degree, bitmapWorkerTask);
	}

}
